package com.sun.batch;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class Producer {
    public static void main(String[] args) throws MQClientException, InterruptedException, RemotingException, MQBrokerException {
        //1、创建消息生产者producer，并制定生产者组名
        DefaultMQProducer producer = new DefaultMQProducer("group1");
        //2、指定Nameserver地址
        producer.setNamesrvAddr("192.168.32.131:9876;192.168.32.132:9876");
        //3、启动producer
        producer.start();

        //4、创建消息对象，指定主题Topic、Tag和消息体
        /**
         * 参数一：消息的主题Topic
         * 参数二：消息的Tag
         * 参数三：消息内容
         */
        List<Message> messages = new ArrayList<>();
        Message msg1 = new Message("BatchTopic","Tag1",("Hello World"+1).getBytes(StandardCharsets.UTF_8));
        Message msg2 = new Message("BatchTopic","Tag1",("Hello World"+2).getBytes(StandardCharsets.UTF_8));
        Message msg3 = new Message("BatchTopic","Tag1",("Hello World"+3).getBytes(StandardCharsets.UTF_8));
        Message msg4 = new Message("BatchTopic","Tag1",("Hello World"+4).getBytes(StandardCharsets.UTF_8));
        Message msg5 = new Message("BatchTopic","Tag1",("Hello World"+5).getBytes(StandardCharsets.UTF_8));
       messages.add(msg1);
       messages.add(msg2);
       messages.add(msg3);
       messages.add(msg4);
       messages.add(msg5);
        //5、发送消息
        SendResult result = producer.send(messages);
        System.out.println("发送详情:"+result);
        //线程睡一秒
        TimeUnit.SECONDS.sleep(1);

        //6、关闭生产者producer
        producer.shutdown();
    }
}
